// index.js
const app = getApp()
Page({
  data: {
    list: [],
    current: 1,
    totalPages: 1,//总页数
    book: {},
    categoryList: [],
    categoryName: '',
    imageList:[],
    imgUrl: '',
    searchValue: '',
  },
  onLoad() {
    this.getCategory()
    this.Book()
    this.setData({
      imgUrl: app.imgUrl
    })
    wx.stopPullDownRefresh()
  },
  onPullDownRefresh(){
    this.onLoad()
},
  Book() {

    wx.request({
      url: app.apiUrl + '/books/list',
      method: "POST",
      data: {
      },
      success: res => {
        this.setData({
          list: res.data.data.records,
          imageList:res.data.data.records,
          categoryName: '',
          searchValue: '',
          current: 1,
          totalPages: res.data.data.pages, // 假设后端返回总页数
        })
      }
    })
  },
  onInput: function (e) {
    // 监听输入框的输入事件，更新 searchValue 数据
    this.setData({
      searchValue: e.detail.value
    });
  },
  navitobook(e) {
    const id = e.currentTarget.dataset.id
    console.log(id + '跳转')
    wx.navigateTo({
      url: '/pages/book/index/index?id=' + id,
    })
  },
 
  getCategory() {
    wx.request({
      url: app.apiUrl + '/books/category',
      method: "GET",
      success: res => {
        console.log(res)
        this.setData({
          categoryList: res.data.data,
        })
      }
    })
  },
  getBook() {
    wx.request({
      url: app.apiUrl + '/books/list',
      method: "POST",
      data: {
        current: this.data.current, // 传递当前页数
        searchValue: this.data.searchValue,
        category: this.data.categoryName
      },
      success: res => {
        console.log(res)
        this.setData({
          // list: res.data.data,
          list: this.data.list.concat(res.data.data.records), // 追加新记录
          totalPages: res.data.data.pages, // 假设后端返回总页数
        })
      }
    })
  },

  // 监听滚动到底部事件
  onReachBottom() {
    if (this.data.current < this.data.totalPages) { // 判断是否还有下一页
      this.setData({
        current: this.data.current + 1 // 增加当前页数
      })

      this.getBook(); // 请求下一页的数据
    } else {
      wx.showToast({
        title: '没有更多书籍',
        icon: 'none'
      });
    }
  },
  getBookPage() {
    wx.request({
      url: app.apiUrl + '/books/list',
      method: "POST",
      data: {
        "current": "",
        "searchValue": this.data.searchValue,
        "category": this.data.categoryName
      },
      success: res => {
        console.log(res)
        this.setData({
          list: res.data.data,
          categoryName: ''
        })
      }
    })
  },
  getSearch() {
    wx.request({
      url: app.apiUrl + '/books/list',
      method: "POST",
      data: {
        "searchValue": this.data.searchValue
      },
      success: res => {
        console.log(res)
        this.setData({
          list: res.data.data.records,
          totalPages: res.data.data.pages,
          current: 1,
          categoryName:''
        })
      }
    })
  },
  categoryByName(e) {
    console.log(e)
    const current = e.currentTarget.dataset.number;
    const category = e.currentTarget.dataset.id;
    wx.request({
      url: app.apiUrl + '/books/list',
      method: "POST",
      data: {
        current,
        category
      },
      success: res => {
        this.setData({
          list: res.data.data.records,
          categoryName: category,
          totalPages: res.data.data.pages,
          current: 1,
        })
      }
    })
  },
  addCart(event) {
    const bookId = event.currentTarget.dataset.id; // 获取 bookId
    this.updateCart(bookId, 1); // 更新后端购物车
  },
  updateCart(bookId, quantityChange) {
    const token = wx.getStorageSync('token');
    if (!token) {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
      });
      return;
    }

    wx.request({
      url: app.apiUrl + '/cart/add',
      method: 'POST',
      header: {
        token: token,
      },
      data: {
        bookId,
        quantity: quantityChange,
      },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
          });
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误，请稍后重试',
          icon: 'error',
        });
      },
    });
  }
})